package org.example.Tool.Component.tContentPanel;

import org.example.Controller.RunTask;
import org.example.Controller.TaskFormNode;
import org.example.Forms.MainForm;
import org.example.Forms.testForm;
import org.example.Tool.Component.CheckCard;
import org.example.Tool.Component.ContentPanel;
import org.example.Tool.Component.MessageCard;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

public class MonitorPanel extends ContentPanel {
    protected int lineSpacing = 100;
    protected int gasp = 5;


    protected static JLabel emptyStats = new JLabel("工作区无任务");

    protected ArrayList<CheckCard> cardList = new ArrayList<>();

    public MonitorPanel(Dimension size){
        super(size);
        this.setLayout(new FlowLayout(FlowLayout.LEFT,gasp,gasp));
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                super.componentResized(e);
                updateCard();
            }
        });
        readTask();
    }

    public void updateCard(){
        for(CheckCard c : cardList) {
            c.setPreferredSize(new Dimension(MonitorPanel.this.getWidth() - gasp * 2, lineSpacing));
            c.repaint();
        }
    }

    public MessageCard addTaskCard(String title, Dimension size){
        MessageCard card = new MessageCard(title);
        card.setPreferredSize(size);
        //card
        Component c = this.add(card);
        cardList.add(card);
        return (MessageCard) c;
    }

    public MessageCard addTaskCard(String title){
        return this.addTaskCard(
                title,
                new Dimension(this.getSize().width - gasp * 2,lineSpacing)
                );
    }

    public void readTask(){
        this.remove(emptyStats);
        if(MainForm.MAIN_TASK.isEmpty()) {
            this.add(emptyStats);
            return;
        }
        Map<Integer,RunTask> tasks = MainForm.MAIN_TASK.stream().filter(
                t->t.type == RunTask.TaskType.DataWatching
        )
                .collect(Collectors.toMap(RunTask::getId,t->t));
        var nodes = MainForm.MAIN_MANAGER.children();
        if(nodes != null)
            while(nodes.hasMoreElements()){
                var node = nodes.nextElement();
                if(node instanceof TaskFormNode){
                    TaskFormNode task_node = ((TaskFormNode) node);
                    if(!tasks.containsKey(task_node.getBindTaskId()))
                        throw new RuntimeException("can't find out task:" + task_node.getBindTaskId());
                    RunTask task = tasks.get(task_node.getBindTaskId());
                    task.formEntrance(this.addTaskCard(task.getName()),task_node);
                    tasks.remove(task_node.getBindTaskId());
                }
            }
        var rest_task = tasks.values();
        rest_task.forEach(
                t->t.formEntrance(this.addTaskCard(t.getName()),new TaskFormNode(testForm::new,MainForm.MAIN_MANAGER,t))
        );
        updateCard();
    }
}
